import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/token.js'
// import Login from '@/views/login.vue'
// import Layout from '@/views/layout.vue'
// import Home from '@/views/home.vue'
// import User from '@/views/usre.vue'
// import Search from '@/views/search.vue'
// import SearchResult from '@/views/SearchResult.vue'
// import ArticleDetail from '@/components/details.vue'
// import UserEdit from '@/components/UserEdit.vue'
// import Chat from '@/views/Chat/index.vue'
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/layout/home' },
  {
    path: '/login',
    component: () => import(/* webpackChunkName: "Login */ '@/views/login.vue'),
    beforeEnter: (to, from, next) => {
      if (getToken()?.length > 0) {
        next('/')
      } else {
        next()
      }
    }
  },
  {
    path: '/search',
    component: () =>
      import(/* webpackChunkName: "Search */ '@/views/search.vue')
  },
  {
    path: '/search_result/:kw',
    component: () =>
      import(/* webpackChunkName: "SearchResult */ '@/views/SearchResult.vue'),
    props: true
  },
  {
    path: '/detail',
    component: () =>
      import(/* webpackChunkName: "ArticleDetail */ '@/components/details.vue')
  },
  {
    path: '/useredit',
    component: () =>
      import(/* webpackChunkName: "UserEdit */ '@/components/UserEdit.vue')
  },
  {
    path: '/chat',
    component: () =>
      import(/* webpackChunkName: "Chat */ '@/views/Chat/index.vue')
  },
  {
    path: '/layout',
    component: () =>
      import(/* webpackChunkName: "Layout */ '@/views/layout.vue'),
    children: [
      {
        path: 'home',
        component: () =>
          import(/* webpackChunkName: "Home */ '@/views/home.vue'),
          meta: {
            scroll: 0
          }
      },
      {
        path: 'user',
        component: () =>
          import(/* webpackChunkName: "User */ '@/views/usre.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// router.beforeEach((to, from, next) => {
//   if (getToken()?.length > 0 && to.path === '/login') {
//     next(false)
//   } else {
//     next()
//   }
// })
export default router
